package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JWTUtil;
import com.example.lotterysystem.common.utils.JacksonUtils;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageListParam;
import com.example.lotterysystem.controller.result.FindPrizeListResult;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController
public class PrizeController {

    private final static Logger logger = LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;

    /**
     * 图片上传
     * @param multipartFile
     * @return
     */
    @RequestMapping("/pic/upload")
    public String upload(MultipartFile multipartFile) {
        return pictureService.savePicture(multipartFile);
    }

    /**
     * 创建奖品
     * @RequestPart：用于接收表单数据
     * @param param
     * @param prizePic
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile prizePic) {
        logger.info("createPrize CreatePrizeParam{}",
                JacksonUtils.writeValueString(param));
        return CommonResult.success(prizeService.createPrize(param,prizePic));

    }

    /**
     * 获取奖品列表
     * @param param
     * @return
     */
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageListParam param) {
        logger.info("findPrizeList param{}",
                JacksonUtils.writeValueString(param));
        PageListDTO<PrizeDTO> prizeListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(converToFindPrizeListResult(prizeListDTO));
    }

    private FindPrizeListResult converToFindPrizeListResult(PageListDTO<PrizeDTO> prizeListDTO) {
        if(null == prizeListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(prizeListDTO.getTotal());
        result.setRecords(prizeListDTO.getRecords().stream()
                .map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo =  new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    return prizeInfo;
                }).collect(Collectors.toList()));
        return  result;
    }

}
